﻿//Created by Action Script Viewer - http://www.buraks.com/asv
package Engine.Managers {
    import flash.events.*;
    import Engine.Interfaces.*;
    import flash.external.*;

    public class InputManager {

        protected static var m_inputHandlers:Array = new Array();
        protected static var m_disableMutex:int = 0;
        protected static var m_lastMouseMoveEvent:MouseEvent;
        private static var m_optimizeMouse:Boolean = true;

        public static function initializeHandlers(_arg1:IEventDispatcher):void{
            var listenObject:* = _arg1;
            listenObject.addEventListener(MouseEvent.MOUSE_UP, onMouseUp);
            listenObject.addEventListener(MouseEvent.MOUSE_DOWN, onMouseDown);
            listenObject.addEventListener(MouseEvent.MOUSE_WHEEL, onMouseWheel);
            listenObject.addEventListener(MouseEvent.DOUBLE_CLICK, onMouseDoubleClick);
            listenObject.addEventListener(MouseEvent.MOUSE_MOVE, onMouseMove);
            listenObject.addEventListener(KeyboardEvent.KEY_DOWN, onKeyDown);
            listenObject.addEventListener(KeyboardEvent.KEY_UP, onKeyUp);
            try {
                if (ExternalInterface.available){
                    ExternalInterface.addCallback("onMouseWheel", onExternalMouseWheel);
                };
            } catch(e:Error) {
            };
        }
        protected static function onExternalMouseWheel(_arg1:int):void{
            onMouseWheel(new MouseEvent(MouseEvent.MOUSE_WHEEL, true, false, 0, 0, null, false, false, false, false, _arg1));
        }
        public static function enableInput():void{
            m_disableMutex--;
            m_disableMutex = Math.max(m_disableMutex, 0);
        }
        public static function disableInput():void{
            m_disableMutex++;
        }
        public static function isInputEnabled():Boolean{
            return ((m_disableMutex == 0));
        }
        public static function addHandler(_arg1:IInputHandler, _arg2:int=0):void{
            m_inputHandlers.splice(_arg2, 0, _arg1);
        }
        public static function removeHandler(_arg1:IInputHandler):void{
            var _local2:int = m_inputHandlers.indexOf(_arg1);
            if ((((_local2 >= 0)) && ((_local2 < m_inputHandlers.length)))){
                m_inputHandlers.splice(_local2, 1);
            };
        }
        public static function onMouseDown(_arg1:MouseEvent):void{
            var _local2:int;
            var _local3:IInputHandler;
            if (isInputEnabled()){
                _local2 = 0;
                while (_local2 < m_inputHandlers.length) {
                    _local3 = (m_inputHandlers[_local2] as IInputHandler);
                    if (((!((_local3 == null))) && (_local3.onMouseDown(_arg1)))){
                        break;
                    };
                    _local2++;
                };
            };
        }
        public static function onMouseUp(_arg1:MouseEvent):void{
            var _local2:int;
            var _local3:IInputHandler;
            if (isInputEnabled()){
                _local2 = 0;
                while (_local2 < m_inputHandlers.length) {
                    _local3 = (m_inputHandlers[_local2] as IInputHandler);
                    if (((!((_local3 == null))) && (_local3.onMouseUp(_arg1)))){
                        break;
                    };
                    _local2++;
                };
            };
        }
        public static function onMouseOut(_arg1:MouseEvent):void{
            var _local2:int;
            var _local3:IInputHandler;
            if (isInputEnabled()){
                _local2 = 0;
                while (_local2 < m_inputHandlers.length) {
                    _local3 = (m_inputHandlers[_local2] as IInputHandler);
                    if (((!((_local3 == null))) && (_local3.onMouseOut(_arg1)))){
                        break;
                    };
                    _local2++;
                };
            };
        }
        public static function checkMouseMove():void{
            var _local1:int;
            var _local2:IInputHandler;
            if (m_lastMouseMoveEvent == null){
                return;
            };
            if (isInputEnabled()){
                _local1 = 0;
                while (_local1 < m_inputHandlers.length) {
                    _local2 = (m_inputHandlers[_local1] as IInputHandler);
                    if (((!((_local2 == null))) && (_local2.onMouseMove(null)))){
                        break;
                    };
                    _local1++;
                };
            };
            m_lastMouseMoveEvent = null;
        }
        public static function onMouseMove(_arg1:MouseEvent):void{
            var _local2:int;
            var _local3:IInputHandler;
            if (m_optimizeMouse){
                m_lastMouseMoveEvent = (_arg1.clone() as MouseEvent);
                return;
            };
            if (isInputEnabled()){
                _local2 = 0;
                while (_local2 < m_inputHandlers.length) {
                    _local3 = (m_inputHandlers[_local2] as IInputHandler);
                    if (((!((_local3 == null))) && (_local3.onMouseMove(_arg1)))){
                        break;
                    };
                    _local2++;
                };
            };
        }
        public static function onMouseDoubleClick(_arg1:MouseEvent):void{
            var _local2:int;
            var _local3:IInputHandler;
            if (isInputEnabled()){
                _local2 = 0;
                while (_local2 < m_inputHandlers.length) {
                    _local3 = (m_inputHandlers[_local2] as IInputHandler);
                    if (((!((_local3 == null))) && (_local3.onMouseDoubleClick(_arg1)))){
                        break;
                    };
                    _local2++;
                };
            };
        }
        public static function onMouseWheel(_arg1:MouseEvent):void{
            var _local2:int;
            var _local3:IInputHandler;
            if (isInputEnabled()){
                _local2 = 0;
                while (_local2 < m_inputHandlers.length) {
                    _local3 = (m_inputHandlers[_local2] as IInputHandler);
                    if (((!((_local3 == null))) && (_local3.onMouseWheel(_arg1)))){
                        break;
                    };
                    _local2++;
                };
            };
        }
        public static function onKeyDown(_arg1:KeyboardEvent):void{
            var _local2:int;
            var _local3:IInputHandler;
            if (isInputEnabled()){
                _local2 = 0;
                while (_local2 < m_inputHandlers.length) {
                    _local3 = (m_inputHandlers[_local2] as IInputHandler);
                    if (((!((_local3 == null))) && (_local3.onKeyDown(_arg1)))){
                        _arg1.stopPropagation();
                        break;
                    };
                    _local2++;
                };
            };
        }
        public static function onKeyUp(_arg1:KeyboardEvent):void{
            var _local2:int;
            var _local3:IInputHandler;
            if (isInputEnabled()){
                _local2 = 0;
                while (_local2 < m_inputHandlers.length) {
                    _local3 = (m_inputHandlers[_local2] as IInputHandler);
                    if (((!((_local3 == null))) && (_local3.onKeyUp(_arg1)))){
                        _arg1.stopPropagation();
                        break;
                    };
                    _local2++;
                };
            };
        }

        public function set optimizeMouse(_arg1:Boolean):void{
            m_optimizeMouse = _arg1;
        }

    }
}//package Engine.Managers 
